﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Manager.App_Code;
using System.Linq;
using Manager.Common;

namespace Manager.FormAdd_Edit
{
    public partial class frm_TamUng : DevExpress.XtraEditors.XtraForm
    {
        NewPearl_HotelDataContext db = new NewPearl_HotelDataContext();
        TamUng tamUng = null;
        int slTamUngTruoc = 0;

        public frm_TamUng()
        {
            InitializeComponent();
        }

        public frm_TamUng(TamUng _tamUng)
        {
            InitializeComponent();
            tamUng = _tamUng;
        }

        private void frm_TamUng_Load(object sender, EventArgs e)
        {
            var lstDichVu = db.DichVus.Where(t => t.NhomDichVu != 4).ToList();//get all dich vu where ten dich vu != "Khác"
            cmbDichVu.DataSource = lstDichVu;
            cmbDichVu.DisplayMember = "TenDichVu";
            cmbDichVu.ValueMember = "MaDV";

            var lstPhong = db.Phongs.ToList();
            cmbPhong.DataSource = lstPhong;
            cmbPhong.DisplayMember = "MaPhong";
            cmbPhong.ValueMember = "TenPhong";
            if (tamUng != null)
            {
                cmbDichVu.SelectedValue = tamUng.MaDichVu;
                cmbPhong.SelectedValue = tamUng.MaPhong;
                if (tamUng.SoLuongTamUng != null)
                {
                    txtTamUng.Text = tamUng.SoLuongTamUng.Value.AddSeparator();
                    slTamUngTruoc = tamUng.SoLuongTamUng.Value;
                }
            }            
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            try
            {
                TamUng _tamUng = null;
                var action = "New";
                if (tamUng != null)
                {
                    action = "Edit";
                    _tamUng = db.TamUngs.SingleOrDefault(t => t.Id == tamUng.Id);
                }
                else
                    _tamUng = new TamUng();
                var dichVu = cmbDichVu.SelectedItem as DichVu;
                _tamUng.DichVu = dichVu;
                _tamUng.Phong = cmbPhong.SelectedItem as Phong;
                _tamUng.SoLuongTamUng = txtTamUng.Text.ParseInt();
                if (action == "New")
                {
                    dichVu.SoLuongTamUng -= txtTamUng.Text.ParseInt();
                }
                else
                {
                    var chenhLech = 0;                    
                    var slTamUngSau = txtTamUng.Text.ParseInt();
                    chenhLech = slTamUngSau - slTamUngTruoc;
                    if (dichVu.SoLuongTamUng != null)
                        dichVu.SoLuongTamUng += chenhLech;
                    else
                        dichVu.SoLuongTamUng = chenhLech;
                }
                db.SubmitChanges();
                this.Close();
            }
            catch(Exception ex)
            {
                Log.WriteLog("btnLuu_Click: " + ex.Message + "\n" + ex.StackTrace);
                MessageBox.Show("Không thể cập nhật được, vui lòng liên hệ ban quản trị.");
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}